package com.educate.common.utils.gen;

import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class GenUtils {


    public static void main(String[] args) throws IOException, TemplateException {
        genProject();
    }

    public static void genProject() throws IOException, TemplateException {
        final String packageName = "org.imperial.golf";
        final String[] tfls = {"dao.ftl", "bo.ftl", "daoImpl.ftl", "service.ftl", "serviceImpl.ftl", "api.ftl"};
//        final String[] tfls = {"dao.ftl", "daoImpl.ftl",  "bo.ftl", "service.ftl", "serviceImpl.ftl"};
        final boolean skipExist = true;

        final String[] modelName = {"Weather"};
        String apiDir = "golf-api";
        String rpcDir = "golf-biz";
        String bizDir = "golf-biz";
        String modelDir = "golf-dao";
        String daoDir = "golf-dao";

        generate(tfls, modelName, skipExist, packageName, apiDir, rpcDir, bizDir, modelDir, daoDir);
    }

    public static String[] getModelNames(String path) {
        String[] list = new File(path).list();
        String[] result = new String[list.length];
        for (int i = 0; i < list.length; i++) {
            String s = list[i];
            result[i] = s.split("\\.")[0];
        }
        return result;
    }

    public static void generate(String[] tfls, String[] modelName, boolean skipExist, String packageName,
                                String apiDir, String rpcDir, String bizDir,
                                String modelDir, String daoDir) throws IOException, TemplateException {
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_27);
        cfg.setTemplateLoader(new ClassTemplateLoader(GenUtils.class, "/template"));
        Map<String, String> viewAndModel = new HashMap<>();
        viewAndModel.put("packageName", packageName);
        for (String model : modelName) {
            for (String tfl : tfls) {
                File file;
                switch (tfl) {
                    case "dao.ftl":
                        file = new File(daoDir, "src/main/java/"
                                + packageName.replace(".", "/")
                                + "/dao/" + model + "Dao.java");
                        break;
                    case "daoImpl.ftl":
                        file = new File(daoDir, "src/main/java/"
                                + packageName.replace(".", "/")
                                + "/dao/impl/" + model + "DaoImpl.java");
                        break;
                    case "bo.ftl":
                        file = new File(modelDir, "src/main/java/"
                                + packageName.replace(".", "/")
                                + "/model/bo/" + model + "BO.java");
                        break;
                    case "service.ftl":
                        file = new File(rpcDir, "src/main/java/"
                                + packageName.replace(".", "/")
                                + "/rpc/api/" + model + "Service.java");
                        break;
                    case "serviceImpl.ftl":
                        file = new File(bizDir, "src/main/java/"
                                + packageName.replace(".", "/")
                                + "/rpc/provide/service/" + model + "ServiceImpl.java");
                        break;
                    case "vo.ftl":
                        file = new File(apiDir, "src/main/java/"
                                + packageName.replace(".", "/")
                                + "/vo/" + model + "VO.java");
                        break;
                    case "api.ftl":
                        file = new File(apiDir, "src/main/java/"
                                + packageName.replace(".", "/")
                                + "/web/controller/" + model + "Controller.java");
                        break;

                    default:
                        throw new RuntimeException();
                }
                if (file.exists()) {
                    if (skipExist) continue;
//                    file = new File(file.getAbsolutePath() + ".temp");
                    file.delete();
                }
                file.getParentFile().mkdirs();
                Template boTemplate = cfg.getTemplate(tfl);
                viewAndModel.put("ModelName", model);
                viewAndModel.put("modelName", getModelNameFirstLowerCase(model));
                FileWriter out = new FileWriter(file);
                boTemplate.process(viewAndModel, out);
                out.flush();
                out.close();
            }
        }
    }

    private static String getModelNameFirstLowerCase(String modelName) {
        return modelName.substring(0, 1).toLowerCase() + modelName.substring(1);
    }
}
